//
//  MVisitorView.swift
//  iOSweibo
//
//  Created by lzm on 16/5/11.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit
//1.定义协议
@objc protocol MVisitorViewDelegate: NSObjectProtocol
{
    //2.协议方法
    //点击注册按钮
    optional func visitorViewDidClickRegister()
    //点击登陆按钮
    optional func visitorViewDidClickLogin()
    
}

class MVisitorView: UIView {
    
    //3.定义代理属性
    weak var delegate:MVisitorViewDelegate?

    //MARK: --修改访客视图内容
    func setVisitorView(imageName:String, message:String)
    {
        //替换转轮
        icomView.image = UIImage(named: imageName)
        //设置message
        messageLabel.text = message
        
        
        //隐藏遮盖
        coverView.hidden = true
        //隐藏小房子
        homeView.hidden = true
    }
    //MARK: --转轮旋转
    func starRotationAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        icomView.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK: --将控件添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        perpareUI()
        
  
    }
    //MARK: --控件与约束
    func perpareUI()
    {
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        self.addSubview(icomView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        
        //取消控件translatesAutoresizingMaskIntoConstraints产生的影响
        icomView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        
        /// CenterX和父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 要添加的View的属性
        // toItem: 要参照的view
        // attribute: 要参照View的属性
        
        //设置转轮约束X轴
        self.addConstraint(NSLayoutConstraint(item: icomView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置转轮约束Y轴
        self.addConstraint(NSLayoutConstraint(item: icomView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //设置遮盖Top
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //设置遮盖Bottom
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //设置遮盖左
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //设置遮盖右
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        //设置小房子X
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置小房子Y
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //设置文字top
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: homeView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //设置文字X
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置文字width
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        //设置注册按钮Top
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        //设置注册按钮左边
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //设置注册按钮宽
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //设置注册按钮高
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置登陆按钮Top
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        //设置登陆按钮右边
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //设置登陆按钮宽
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //设置登陆按钮高
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//MARK: --点击按钮事件
    //注册
    @objc private func didClickRegister()
    {
        //4.调用代理方法
        delegate?.visitorViewDidClickRegister?()
    }
    @objc private func didClicKLogin()
    {
        delegate?.visitorViewDidClickLogin?()
    }
    
    
    //MARK: --懒加载访客控件
    //转轮
    private lazy var icomView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,看看有什么惊喜!"
        label.font = UIFont.systemFontOfSize(16)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn:UIButton = {
       let registerBtn = UIButton()
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.addTarget(self, action: "didClickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return registerBtn
    }()
    
    //登陆按钮
    private lazy var loginBtn: UIButton = {
        let loginBtn = UIButton()
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        loginBtn.setTitle("登陆", forState: UIControlState.Normal)
        loginBtn.addTarget(self, action: "didClicKLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    

}
